package com.educal.Controller;

import com.educal.Bean.Admin;
import com.educal.Repository.AdminRepository;
import com.educal.Repository.StuRepository;
import com.educal.Bean.Student;
import com.educal.Bean.Teacher;
import com.educal.Repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by miyang on 2017/11/23.
 */
@Controller
public class UserController {

    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private StuRepository stuRepository;
    @Autowired
    private TeacherRepository teacherRepository;
  //  Session session = new HttpSession();
    /*
    * 在界面获取用户id、密码、权限与数据库中的比较，
    * 成功返回200，错误返回401
    * */
    @RequestMapping("/go")
      public String go(){
          return "test2";
      }

    //@RequestMapping(value = "id",method = RequestMethod.GET)
    //@ResponseBody
    //@RequestMapping(value = "/users",method = RequestMethod.POST)
   @PostMapping("/users")
    @ResponseBody
    public Map<String,Integer> login(@RequestParam("usrid") String usrid,
                                      @RequestParam("pwd") String pwd,
                                      @RequestParam("auth") Integer auth){
        Integer flag = 401;//表示登录失败
        Map<String,Integer> result = new HashMap<String,Integer>();
        result.put("code",flag);
       System.out.print(usrid + pwd + auth);
        if(auth.equals(1)){
            Admin admin = new Admin();
            List<Admin> list = adminRepository.findAll();
            for(int i=0; i<list.size();i++) {
                String userId = list.get(i).getAdminid();
                String userPwd = list.get(i).getAdminpassword();

                //   System.out.print(userId + userPwd + userAuth);
                if (usrid.equals(userId) && pwd.equals(userPwd)) {
                    flag = 200; //表示登录成功
                    result.put("code", flag);
                    /*session.setAttribute("id",id);
                    session.setAttribute("password",password);
                    session.setAttribute("auth",auth);*/
                    break;
                }
            }
        }else if(auth.equals(2)){
            List<Teacher> list = teacherRepository.findAll();
            for(int i=0; i<list.size();i++) {
                String userId = list.get(i).getTid();
                String userPwd = list.get(i).getTpassword();

                //   System.out.print(userId + userPwd + userAuth);
                if (usrid.equals(userId) && pwd.equals(userPwd)) {
                    flag = 200; //表示登录成功
                    result.put("code", flag);
                    /*session.setAttribute("id",id);
                    session.setAttribute("password",password);
                    session.setAttribute("auth",auth);*/
                    break;
                }
            }
        }else if(auth.equals(3)){
            List<Student> list = stuRepository.findAll();
            for(int i=0; i<list.size();i++) {
                String userId = list.get(i).getStuid();
                String userPwd = list.get(i).getStupassword();

                //   System.out.print(userId + userPwd + userAuth);
                if (usrid.equals(userId) && pwd.equals(userPwd)) {
                    flag = 200; //表示登录成功
                    result.put("code", flag);
                    /*session.setAttribute("id",id);
                    session.setAttribute("password",password);
                    session.setAttribute("auth",auth);*/
                    break;
                }
            }
        }
        return result;
    }

/*    public Map<String,Integer> logout(@RequestParam("id") String id,
                                     @RequestParam("password") String password,
                                     @RequestParam("auth") Integer auth){
        Integer flag = 401;//表示注销失败
        Map<String,Integer> result = new HashMap<String,Integer>();
        result.put("code",flag);
        System.out.print(id + password + auth);
        List<User> list = userRepository.findAll();
        for(int i=0; i<list.size();i++){
            String userId = list.get(i).getUserid();
            String userPwd = list.get(i).getUserpassword();
            Integer userAuth = list.get(i).getUserauth();

            System.out.print(userId + userPwd + userAuth);
            if(id.equals(userId) && password.equals(userPwd) && auth.equals(userAuth)){
                flag = 200; //表示注销成功
                result.put("code",flag);
                break;
            }
        }
        return result;
    }*/

}
